//
//  AddressTableViewCell.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/7/25.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "AddressTableViewCell.h"

@implementation AddressTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = BackgroundCOLOR;
    }
    return self;
}

- (void)setView:(AddressModel *)model{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1000)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    
    //姓名
    _name = [[UILabel alloc] initWithFrame:CGRectMake(12, 12, 100, 20)];
    _name.font = [UIFont systemFontOfSize:16];
    _name.text = model.name;
    _name.numberOfLines = 1;
    [_name sizeToFit];
    [bgView addSubview:_name];
    
    //联系电话
    _phone = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 200, 12, 200, 20)];
    _phone.font = [UIFont systemFontOfSize:16];
    _phone.text = model.phone;
    _phone.textAlignment = NSTextAlignmentRight;
    _phone.numberOfLines = 1;
    [_phone sizeToFit];
    CGFloat width = _phone.frame.size.width;
    _phone.frame = CGRectMake(SCREEN_WIDTH - 12 - width, 12, width, 20);
    [bgView addSubview:_phone];
    
    //地址
    _address = [[UILabel alloc] initWithFrame:CGRectMake(12, 50, SCREEN_WIDTH - 20, 100)];
    _address.font = [UIFont systemFontOfSize:16];
    _address.numberOfLines = 0;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.address];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.address length])];
    _address.attributedText = attributedString;
    [_address sizeToFit];
    [bgView addSubview:_address];
    
    //分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, _address.frame.size.height + _address.frame.origin.y + 10, SCREEN_WIDTH, 1)];
    line.backgroundColor = BackgroundCOLOR;
    [bgView addSubview:line];
    
    //设为默认Button
    _defaults = [UIButton buttonWithType:UIButtonTypeCustom];
    _defaults.frame = CGRectMake(0, line.frame.origin.y + 1, 100, 50);
    [_defaults addTarget:self action:@selector(setDefaultsAddress) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_defaults];
    
    //默认Image
    _defaultsImage = [[UIImageView alloc] initWithFrame:CGRectMake(12, 15, 20, 20)];
    if ([model.defaults isEqualToString:@"1"]) {
        _defaultsImage.image = [UIImage imageNamed:@"GLDZicon_08"];
    }else _defaultsImage.image = [UIImage imageNamed:@"GLDZicon_13"];
    [_defaults addSubview:_defaultsImage];
    
    //默认Label
    _defaultsLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 15, 56, 20)];
    _defaultsLabel.font = [UIFont systemFontOfSize:14];
    if ([model.defaults isEqualToString:@"1"]) {
        _defaultsLabel.text = @"默认地址";
        _defaultsLabel.textColor = APPCOLOR;
    }else{
        _defaultsLabel.text = @"设为默认";
        _defaultsLabel.textColor = [UIColor blackColor];
    }
    [_defaults addSubview:_defaultsLabel];
    
    //编辑button
    _edit = [UIButton buttonWithType:UIButtonTypeCustom];
    _edit.frame = CGRectMake(SCREEN_WIDTH - 160, line.frame.origin.y + 1, 80, 50);
    [_edit addTarget:self action:@selector(editAddress) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_edit];
    
    //编辑Image
    UIImageView *editImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 15, 20, 20)];
    editImage.image = [UIImage imageNamed:@"GLDZicon_03"];
    [_edit addSubview:editImage];
    
    //编辑Label
    UILabel *editLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 28, 50)];
    editLabel.text = @"编辑";
    editLabel.font = [UIFont systemFontOfSize:14];
    [_edit addSubview:editLabel];
    
    //删除按钮
    _deletes = [UIButton buttonWithType:UIButtonTypeCustom];
    _deletes.frame = CGRectMake(SCREEN_WIDTH - 80, line.frame.origin.y + 1, 80, 50);
    [_deletes addTarget:self action:@selector(deleteAddress) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_deletes];
    
    //删除Image
    UIImageView *deleteImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 15, 20, 20)];
    deleteImage.image = [UIImage imageNamed:@"GLDZicon_05"];
    [_deletes addSubview:deleteImage];
    
    //删除Label
    UILabel *deleteLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 28, 50)];
    deleteLabel.text = @"删除";
    deleteLabel.font = [UIFont systemFontOfSize:14];
    [_deletes addSubview:deleteLabel];
    
    bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, _deletes.frame.origin.y + 50);
    _height = bgView.frame.size.height;
}

//设置默认地址
- (void)setDefaultsAddress{
    _addressClick(0);
}
//编辑
- (void)editAddress{
    _addressClick(1);
}
//删除
- (void)deleteAddress{
    _addressClick(2);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
